﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain;
using Zero.Core.Domain.Query;
using Zero.DataLayer.Donnees;
using Zero.POCO;

namespace Zero.DataLayer.Repository
{
    public class AnnonceurRepository : IRepository<Annonceur, Guid>
    {
        public Annonceur FindBy(Guid id)
        {

            var config = new AppSettingsReader();
            using (
                     var xpoSession = new Session()
                     {
                         ConnectionString =
                             ((string)config.GetValue("connect", typeof(string)))
                     })
            {
                var instance = xpoSession.GetObjectByKey<XpoAnnonceur>(id);
                if (instance == null)
                    return null;
                return MAP.MapInverse.MapAnnonceur(instance);
            }
        }

        public IEnumerable<Annonceur> FindAll()
        {
            var listAnnonceur = new List<Annonceur>();

            var config = new AppSettingsReader();
            using (
               var uow = new UnitOfWork()
               {
                   ConnectionString =
                       ((string)config.GetValue("connect", typeof(string)))
               })
            {
                var entities = new XPCollection<XpoAnnonceur>(uow);
                listAnnonceur.AddRange(entities.Select(MAP.MapInverse.MapAnnonceur));
            }
            return listAnnonceur;
        }

        public IEnumerable<Annonceur> FindBy(Query query)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Annonceur> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }

        public void SaveAll(IEnumerable<Annonceur> entities)
        {
            throw new NotImplementedException();
        }

        public List<Annonceur> FindAll(List<Query> entities)
        {
            throw new NotImplementedException();
        }

        public void Save(Annonceur entity)
        {

            var config = new AppSettingsReader();
            using (
                      var uow = new UnitOfWork()
                      {
                          ConnectionString =
                              ((string)config.GetValue("connect", typeof(string)))
                      })
            {
                var xpoAnnonceur = uow.GetObjectByKey<XpoAnnonceur>(entity.id);
                if (xpoAnnonceur == null)
                {
                    var annonceur= MAP.Map.MapAnnonceur(entity, uow);
                }
                else
                {
                    
                }

                uow.CommitChanges();
            }
        }

        public void Add(Annonceur entity)
        {
            throw new NotImplementedException();
        }

        public void Remove(Annonceur entity)
        {
            throw new NotImplementedException();
        }
    }
}
